package com.ydt.dockingclients.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.*;

/**
 * 时间工具类
 *
 * @author 多乐汇
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";
    public static String YYYYMMDD = "yyyyMMdd";
    public static String YYYYtMMtDD = "yyyy.MM.dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

    public static String STANDARD_DATE_FORMAT_UTC = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

        return sdf.format(date);
    }

    public static Instant parseDateToInstant(String format, String ts) {
        Date date = dateTime(format, ts);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.toInstant();
    }

    /**
     * 获取日历集合
     *
     * @param page
     * @param size
     * @return
     */
    public static List<String> pageDates(int page, int size) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        List<String> dates = new ArrayList<>();
        int ci = page == 0 ? 0 : page * size;
        for (int i = 0; i < size; i++) {
            calendar.setTime(new Date());
            calendar.add(Calendar.DAY_OF_YEAR, -ci);
            dates.add(DateUtils.parseDateToStr(YYYY_MM_DD, calendar.getTime()));
            ci++;
        }
        return dates;
    }

    /**
     * 当前日期+addDay
     *
     * @param addDay
     * @return
     */
    public static String dateAddDay(Integer addDay) {
        Date date = dateTime(YYYY_MM_DD, getDate());
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, addDay);
        return parseDateToStr(YYYY_MM_DD, calendar.getTime());
    }

    public static final String parseDateToStr(final String format, final Instant instant) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        return sdf.format(new Date(instant.toEpochMilli()));
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    public static final String thisDate() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * Instant转Date
     *
     * @param instant
     * @return
     */
    public static Date toDate(Instant instant) {
        BigInteger milis = BigInteger.valueOf(instant.getEpochSecond()).multiply(
                BigInteger.valueOf(1000));
        milis = milis.add(BigInteger.valueOf(instant.getNano()).divide(
                BigInteger.valueOf(1_000_000)));
        return new Date(milis.longValue());
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }


    /**
     * 计算时间相差小时
     *
     * @param time
     * @return
     */
    public static Integer getDatePoor(Date time) {
        long nh = 1000 * 60 * 60;
        Long diff = new Date().getTime() - time.getTime();
        Long hours = diff / nh;
        return hours.intValue();
    }

    public static Integer getDatePoorByMin(Date time) {
        long nm = 1000 * 60;
//        Calendar calendar = Calendar.getInstance();
//        calendar.set(2024,1,1,15,39,0);
//        Date time  = calendar.getTime();
        Long diff = new Date().getTime() - time.getTime();
        Long min = diff / nm;
        return min.intValue();
    }

    /**
     * 获取昨天
     *
     * @return
     */
    public static String getYestTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getNowDate());
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        // 昨日
        return DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, calendar.getTime());
    }

    /**
     * 本月开始
     *
     * @return
     */
    public static Date getFirstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        // 将日期设置为本月第一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取本月最后一天
     *
     * @return
     */
    public static Date getLastDayOfMonth() {
        // 将日期设置为本月第一天
        Calendar cal = Calendar.getInstance();
        // 将日期设置为本月第一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        // 将日期加上一个月，然后再减去一天得到本月最后一天
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DATE, -1);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);

        return cal.getTime();
    }

    /**
     * 获取当天(day)开始时间
     *
     * @return
     */
    public static Date getFirstDayOfDay(int day) {
        Calendar begin = Calendar.getInstance();
        begin.setTime(getNowDate());
        begin.add(Calendar.DAY_OF_YEAR, day);
        begin.set(Calendar.HOUR_OF_DAY, 0);
        begin.set(Calendar.MINUTE, 0);
        begin.set(Calendar.SECOND, 0);
        return begin.getTime();

    }

    /**
     * 获取当天(day)结束时间
     *
     * @return
     */
    public static Date getLastDayOfDay(int day) {
        Calendar end = Calendar.getInstance();
        end.setTime(getNowDate());
        end.add(Calendar.DAY_OF_YEAR, day);
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        return end.getTime();
    }

    public static Date getLastDayOfHour(int hour) {
        Calendar end = Calendar.getInstance();
        end.setTime(getNowDate());
        end.add(Calendar.HOUR, hour);
        return end.getTime();
    }
}
